package cn.hfatec.shms.system.role.service;

import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.base.BaseDao;
import cn.hfatec.shms.advice.annotation.MysqlDataSource;
import cn.hfatec.shms.exception.KeyGenException;
import cn.hfatec.shms.system.role.dao.RoleDao;
import cn.hfatec.shms.system.role.model.Role;
import cn.hfatec.shms.system.role.model.RoleParams;
import cn.hfatec.shms.system.roleAuth.dao.RoleAuthDao;
import cn.hfatec.shms.system.roleMenu.dao.RoleMenuDao;
import cn.hfatec.shms.system.userRole.dao.UserRoleDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("RoleService")
public class RoleService {

    //注入Dao层
    @Resource(name = "RoleDao")
    private RoleDao roleDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    @Resource(name = "UserRoleDao")
    private UserRoleDao userRoleDao;

    @Resource(name = "RoleAuthDao")
    private RoleAuthDao roleAuthDao;

    @Resource(name = "RoleMenuDao")
    private RoleMenuDao roleMenuDao;


    /**
     *
     * 查询数据
     *
     * @param roleParams
     * @param page
     *
     */
    @Transactional
    @MysqlDataSource
    public Table queryRoleTable(RoleParams roleParams, Page page){

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", roleParams);
        return baseDao.queryTableByPage("queryRole", params, page);
    }

    /**
     *
     * 查询角色列表
     *
     * @param roleParams
     * @return
     */
    @Transactional
    @MysqlDataSource
    public List queryRoleList(RoleParams roleParams){

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", roleParams);
        return baseDao.queryList("queryRole", params);
    }


    /**
     *
     * 插入数据
     * @param role
     *
     */
    @Transactional
    @MysqlDataSource
    public void saveRole(Role role) throws KeyGenException {
        if(role.getId() != null) {
            Role result = getRole(role.getId());
            if(result != null) {
                roleDao.update(role);
            } else {
                roleDao.insert(role);
            }
        } else {
            roleDao.insert(role);
        }
    }

    /**
     *
     * 删除数据
     * @param id
     *
     */
    @Transactional
    @MysqlDataSource
    public void deleteRole(Integer id) {
        Role role = getRole(id);
        userRoleDao.deleteUserRoleBatchByRoleId(role.getRoleId());
        roleAuthDao.deleteRoleAuthBatchByRoleId(role.getRoleId());
        roleMenuDao.deleteRoleMenuBatch(role.getRoleId());
        roleDao.delete(id);
    }

    /**
     *
     * 删除数据
     * @param roleList
     *
     */
    @Transactional
    @MysqlDataSource
    public void deleteRoleBatch(List<String> roleList) {
        for(String id : roleList) {
            deleteRole(Integer.valueOf(id));
        }
    }

    /**
     *
     * 根据ID获取数据
     * @param id
     *
     */
    @Transactional
    @MysqlDataSource
    public Role getRole(Integer id) {

        return roleDao.getRoleById(id);
    }
}